package com.example.seckillweb.config;

import com.example.seckillweb.mapper.Goods;
import com.example.seckillweb.mapper.GoodsMapper;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @description: 定时任务，同步DB库存到Redis中
 * @author: uu
 * @date: 2023/12/18 21:30
 * @version: 1.0
 */
@Component
public class DataSync {
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

//    @Scheduled(cron = "0 0 10 * * ?")
    @PostConstruct
    public void initData(){
        List<Goods> goods = goodsMapper.findSecKillGoods();
        if (CollectionUtils.isEmpty(goods))
            return;
        goods.forEach(good -> {
            redisTemplate.boundValueOps("gd:" + good.getGoodsId()).set(good.getTotalStocks().toString());
//            redisTemplate.opsForValue().set("gd:" + good.getGoodsId(), good.getTotalStocks());
        });
    }
}
